/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.report.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.service.PaymentService;
import com.rzico.account.service.ReceivableService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.service.*;
import com.rzico.core.entity.SysEmployee;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.*;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.report.entity.ShippingDelVo;
import com.rzico.report.service.ShippingDelService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 送货单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "送货单接口")
@RestController
@RequestMapping("/report/shippingDel")
public class ShippingDelController extends BaseController {

    @Autowired
    private ShippingDelService shippingDelService;

    @Autowired
    protected SysEmployeeService sysEmployeeService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserDataScopeService sysUserDataScopeService;



    /**
     * 分页查询配送报表
     *
     * @return
     */
    @ApiOperation("分页查询配送报表")
    @GetMapping("/selectShippingDelList")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "supplierId", value = "供应商ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "收货电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "配送单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderSn", value = "销售单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shippingStatus", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startCompleteDate", value = "开始完成时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endCompleteDate", value = "结束完成时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "企业ID", dataType = "Long", paramType = "query")
    })
    public CommResult<ShippingDelVo> selectShippingDelList(Date startDate, Date endDate, String sn, Long supplierId, Long memberId, Integer shippingStatus, String orderSn,
                                                           Long productId, String phone, String startCompleteDate, String endCompleteDate, Long companyId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (StringUtils.isNotEmpty(sn)){
            params.put("sn", sn);
        }
        if (StringUtils.isNotEmpty(orderSn)){
            params.put("orderSn", orderSn);
        }
        if (null != productId){
            Product product = productService.findById(productId);
            params.put("goodsId", product.getGoodsId());
        }
        if (null != phone){
            params.put("phone", phone);
        }
        if (memberId!=null) {
            params.put("memberId",memberId);
        }
        if (StringUtils.isNotEmpty(startCompleteDate)){
            params.put("startCompleteDate", startCompleteDate);
        }
        if (StringUtils.isNotEmpty(endCompleteDate)){
            params.put("endCompleteDate", endCompleteDate);
        }

        params.put("deleted", false);

        if (shippingStatus!=null) {
            params.put("shippingStatus",shippingStatus);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        //检查是否供应商账号
        SysUser sysUser = sysUserService.getCurrent();
        Supplier supplier = supplierService.findByUserId(sysUser.getId());
        if (supplier!=null) {
            params.put("supplierId",supplier.getId());
        } else {
            //非管理员都启用数据权限
            if (!sysUserService.isAdmin(sysUser)) {
                //params.put("dataScopeId",sysUser.getId());
                if (sysUserDataScopeService.checkHasPermissio(sysUser.getId(),2)) {
                    if (sysUser.getMchId().equals(enterprise.getMchId())) {
                        params.put("shopScopeId", sysUser.getId());
                    }
                } else {
                    SysEmployee sysEmployee = sysEmployeeService.getCurrent();
                    if (sysEmployee==null) {
                        return CommResult.error("无效员工账号");
                    }
                    params.put("shippingEmployeeId",sysEmployee.getId());
                }
            }
        }

        if (supplierId!=null){
            params.put("supplierId", supplierId);
        }
        if (companyId!=null){
            params.put("companyId", companyId);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ShippingDelVo> list = shippingDelService.selectShippingDelList(params);
        PageResult<ShippingDelVo> pageResult = new PageResult<ShippingDelVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
